import _ from "lodash";
import fs from "fs";
import { exec } from "node:child_process";

// _path必须是Bot根目录的绝对路径，路径不能带有软链接
const _path = `${process.cwd()}`;
const gachaPath = `${_path}/data/gachaJson/`;

const transCp = async (e, Qid, Gid) => {
  const target = `${gachaPath}/${Gid}`;
  const source = `${gachaPath}/${Qid}`;
  const cmd = `ln -s ${source} ${target}`;

  if (!fs.existsSync(source))
    try {
      await e.reply(`本地没有QQ${Qid}的抽卡记录`);
      return false;
    } catch (err) {
      logger.error(logger.red(err));
    }
  if (fs.existsSync(target))
    try {
      await e.reply("QQ已存在");
      logger.error(`${logger.red("目录已存在，无法创建链接")}`);
      return false;
    } catch (err) {
      logger.error(logger.red(err));
    }
  exec(cmd, async (error, stdout, stderr) => {
    if (error) {
      await e.reply(`创建链接失败:\n${error}`);
      return false;
    }
    logger.info(`stdout: ${stdout}`);
    logger.error(`stderr: ${logger.red(stderr)}`);
  });
  await e.reply(`${Qid}绑定频道成功喵~`);
};

const transDel = async (e, Qid, Gid) => {
  await e.reply(`成功删除喵~`);
};

export { transCp, transDel };
